package com.cetool.autocms.util;

import cn.hutool.core.date.DateUtil;
import com.cetool.autocms.enumeration.SystemExceptionEnum;
import com.cetool.autocms.exception.ServiceException;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author 戴越
 * @date 2021/11/29
 */
public class FileUpLoadUtil {
    public static String fileUpload(MultipartFile file, String filePath) throws IOException {
        if (null == file || file.isEmpty()){
            throw new ServiceException(SystemExceptionEnum.FILEUPLOAD_FALSE_FILE_ISNULL);
        }
        File fileDir = new File(filePath);
        if (!fileDir.exists()){
            fileDir.mkdirs();
        }
        String filename = file.getOriginalFilename();
        // 文件后缀 例如：.png
        String fileSuffix = filename.substring(filename.lastIndexOf("."));
        filename = filePath + "chart_001" + fileSuffix;
        File dest = new File(filename);
        file.transferTo(dest);
        return filename;
    }
}
